package logic

import (
	"context"
	"doctor-medicine-system/models/mysql"
	"errors"

	"doctor-medicine-system/medrpc/internal/svc"
	"doctor-medicine-system/medrpc/medrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *medrpc.LoginRequest) (*medrpc.LoginResponse, error) {
	// todo: add your logic here and delete this line

	var doctor mysql.Doctor

	err := doctor.FindDoctorByCode(mysql.DB, in.DocCode)
	if err != nil {
		return nil, errors.New("该医生不存在")
	}

	if doctor.Password != in.Passwprd {
		return nil, errors.New("密码错误")
	}

	return &medrpc.LoginResponse{
		DocId: int64(doctor.ID),
	}, nil
}
